package com.huike.web.controller.report;


import com.huike.report.domain.vo.TodayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huike.common.core.domain.AjaxResult;
import com.huike.report.service.IReportService;

import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IReportService reportService;


    /**
     * 首页--基础数据统计
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/getBaseInfo")
    public AjaxResult getBaseInfo(@RequestParam("beginCreateTime") String beginCreateTime,
                                  @RequestParam("endCreateTime") String endCreateTime){
        return AjaxResult.success(reportService.getBaseInfo(beginCreateTime,endCreateTime));
    }

    /**
     * 获取今日简报
     *
     * @return 今日简报统计数据
     */
    @GetMapping("/getTodayInfo")
    public AjaxResult getTodayInfoVo() {
/*        int todayCluesNum = reportService.getTodayCluesNum();
        int todayBusinessNum = reportService.getTodayBusinessNum();
        int todayContractNum = reportService.getTodayContractNum();
        double todaySalesAmount = reportService.getTodaySalesAmount();*/

        TodayInfoVO todayInfoVO = new TodayInfoVO();
/*        todayInfoVO.setTodayCluesNum(todayCluesNum);
        todayInfoVO.setTodayBusinessNum(todayBusinessNum);
        todayInfoVO.setTodayContractNum(todayContractNum);
        todayInfoVO.setTodaySalesAmount(todaySalesAmount);*/


        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> {return null;});
        CompletableFuture<Integer> cluesNumFuture = CompletableFuture.supplyAsync(() -> {
            return reportService.getTodayCluesNum();
        });

        CompletableFuture<Integer> businessNumFuture = CompletableFuture.supplyAsync(() -> {
            return reportService.getTodayBusinessNum();
        });

        CompletableFuture<Integer> contractNumFuture = CompletableFuture.supplyAsync(() -> {
            return reportService.getTodayContractNum();
        });

        CompletableFuture<Double> salesAmountFuture = CompletableFuture.supplyAsync(() -> {
            return reportService.getTodaySalesAmount();
        });

        CompletableFuture.allOf(cluesNumFuture, businessNumFuture, contractNumFuture, salesAmountFuture).join();
        try {
            todayInfoVO.setTodayCluesNum(cluesNumFuture.get());
            todayInfoVO.setTodayBusinessNum(businessNumFuture.get());
            todayInfoVO.setTodayContractNum(contractNumFuture.get());
            todayInfoVO.setTodaySalesAmount(salesAmountFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return AjaxResult.success(todayInfoVO);
    }
}
